#include "CCanvas.h"
#include "Base.h"
#include "Timer.h"

using namespace std;

//-----------------------------------------------------------------------------

void CCanvas::initializeGL()
{
  glClearColor(0.0f, 0.0f, 0.0f, 0.5f);			          // Black Background
  glClearDepth(1.0f);									                // Depth Buffer Setup
  glEnable(GL_DEPTH_TEST);							              // Enables Depth Testing
  glDepthFunc(GL_LEQUAL);								              // The Type Of Depth Testing To Do
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
};

//-----------------------------------------------------------------------------

void CCanvas::resizeGL(int width, int height)
{
  // set up the window-to-viewport transformation
  glViewport( 0,0, width,height );

  // vertical camera opening angle
  double beta = 90.0;

  // aspect ratio
  double gamma;
  if (height > 0)
    gamma = width/(double)height;
  else
    gamma = width;

  // front and back clipping plane at
  double n = -1.0;
  double f = -10.0;
  
  // frustum corners
  double t = -tan(beta*3.14159/360.0) * n;
  double b = -t;
  double r = gamma * t;
  double l = -r;

  // set projection matrix
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum( l,r , b,t , -n,-f );

  // alternatively, directly from alpha and gamma
  //gluPerspective( beta, gamma, -n, -f );
};

//-----------------------------------------------------------------------------

void CCanvas::paintGL()
{
  // clear screen and depth buffer
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // set model-view matrix
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt( 0,0,0,  0,0,-1,  0,1,0 );     // camera position , "look at" point , view-up vector

  // set up a sphere
  GLUquadricObj *sphere;
  sphere = gluNewQuadric();
  gluQuadricDrawStyle( sphere, GLU_LINE );

  // transform and draw sphere
  glTranslated (0.5, 0.5, -5.0);	
	glScaled (2.0, 2.0, 2.0);
	glRotated ( tau, 1,0,0 );
  gluSphere( sphere, 1.0, 20,10 );

  tau += 1.0;
}